package com.example.fp.zbs.controller;

import com.example.fp.cgc.code.logAnno.LogAnno;
import com.example.fp.zbs.entity.Overseas;
import com.example.fp.zbs.mapper.OverseasMapper;

import com.example.fp.zlz.entity.Game;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping
public class OverseasWebController {
    @Autowired
    private OverseasMapper overseasMapper;

    //测试
    @LogAnno(operateType = "查询当前应用列表")
    @RequestMapping(value ="/overseaslist")
    public ModelAndView getallOverseas(Model model){
        List<Overseas> overseasList=overseasMapper.selectalloverseas();
        model.addAttribute("overseasList", overseasList);
        return new ModelAndView("overseasweb/overseaslist", "overseasModel", model);
    }

    @RequestMapping(value ="/oveseassuccess")
    public ModelAndView succOverseas(Model model){
        return new ModelAndView("overseasweb/overseassuccess", "overseasModel", model);
    }


    @GetMapping(value ="/overseasForm")
    public ModelAndView createOverseasForm(Model model){
        model.addAttribute("overseas", new Overseas());
        model.addAttribute("title", "创建一个新的海外应用");
        return new ModelAndView("overseasweb/addoverseas", "overseasModel", model);
    }
    @PostMapping(value ="/addOrModityOverseas")
    public ModelAndView saveOrUpdateOverseas(Overseas overseas){
        Overseas overseas1= overseasMapper.selectById(overseas.getId());

        if (overseas1 == null) {
            overseasMapper.insert(overseas);
            return new ModelAndView("redirect:/overseassuccess");//重定向到list页面
        } else {
            overseasMapper.updateById(overseas);
            return new ModelAndView("redirect:/overseaslist"); //重定向到list页面
        }
    }
    @GetMapping("/deleteOverseas/{id}")
    public ModelAndView deleteOverseas(@PathVariable("id") int id){
        overseasMapper.deleteById(id);
        return new  ModelAndView("redirect:/overseaslist");//重定向到list页面
    }

    @GetMapping("/modifyOverseas/{id}")
    public ModelAndView modifyOverseas(@PathVariable("id") int id,Model model){
        model.addAttribute("overseas", overseasMapper.selectById(id));
        model.addAttribute("title", "修改海外应用");
        return new ModelAndView("overseasweb/modifyOverseas", "overseasModel", model);
    }
}
